package ru.osu.acm.testing.beans;

import ru.osu.acm.common.entities.Message;
import ru.osu.acm.common.entities.Problem;
import ru.osu.acm.common.entities.Team;
import ru.osu.acm.common.entities.Person;
import ru.osu.acm.common.industrial.MessageFactory;
import ru.osu.acm.common.industrial.ProblemFactory;
import ru.osu.acm.common.industrial.ContestFactory;
import ru.osu.acm.common.industrial.TeamFactory;
import ru.osu.acm.common.config.Config;
import ru.osu.acm.testing.Utils;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Date;

/**
 * User: aleksey
 * Date: Oct 1, 2009
 * Time: 10:55:10 PM
 */
public class MessageBean {
    @Resource(name = Config.jndi)
    private DataSource source;

    private UserBean getUser() {
        return DataBean.getUserBean();
    }

    private int getContestId() {
        return DataBean.getContestId();
    }

    public ArrayList<Message> getMessages() {
        if (getUser().isTeam()) {
            return MessageFactory.getTeamMessages(getUser().getTeam().getId(), Utils.getConnection(source), true);
        }
        return MessageFactory.getUserMessages(getUser().getPerson().getId(), Utils.getConnection(source), true);
    }

    public int getMessageCount() {
        if (getUser().isTeam()) {
            return MessageFactory.getTeamMessageCount(getUser().getTeam().getId(), Utils.getConnection(source), true);
        }
        return MessageFactory.getUserMessageCount(getUser().getPerson().getId(), Utils.getConnection(source), true);
    }

    public String getLinkStyle() {
        return ""; // TODO: bold for unread
    }

    private int selectedProblem = -1;

    public int getSelectedProblem() {
        return selectedProblem;
    }

    public void setSelectedProblem(int selectedProblem) {
        this.selectedProblem = selectedProblem;
    }

    public int getProblemCount() {
        return ProblemFactory.getContestProblemCount(getContestId(), Utils.getConnection(source), true);
    }

    public ArrayList<SelectItem> getProblems() {
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        ArrayList<Problem> problems = ProblemFactory.getContestProblems(getContestId(), Utils.getConnection(source), true);
        for (Problem problem : problems) {
            items.add(new SelectItem(problem.getId(), problem.getLetterWithPoint() + problem.getName()));
        }
        if (selectedProblem == -1 && problems.size() > 0) {
            selectedProblem = problems.get(0).getId();
        }
        return items;
    }

    private String newMessage;

    public String getNewMessage() {
        return newMessage;
    }

    public void setNewMessage(String newMessage) {
        this.newMessage = newMessage;
    }

    // Action handlers

    public String submit() {
        Message message = new Message(-1, getUser().isTeam() ? new Team(getUser().getTeam().getId(), "", "", "") : null,
                getUser().isTeam() ? null : new Person(getUser().getPerson().getId(), "", "", "", "", false),
                ContestFactory.getContest(getContestId(), Utils.getConnection(source), true),
                ProblemFactory.getProblem(selectedProblem, Utils.getConnection(source), true),
                false, newMessage, new Date());
        MessageFactory.updateMessage(message, Utils.getConnection(source), true);
        return null;
    }
}
